using System;
using System.IO;
using System.Globalization;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Xml;

namespace Website
{
    public sealed class CreatedActionResult : ActionResult
    {
        public CreatedActionResult(string location)
        {
            if (null == location)
            {
                throw new ArgumentNullException("location");
            }

            if (0 == location.Length)
            {
                throw new ArgumentOutOfRangeException("location");
            }

            this.Location = location;
        }

        public string Location
        {
            get;
            set;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            if (null == context)
            {
                throw new ArgumentNullException("context");
            }

            HttpResponseBase response = context.HttpContext.Response;

            Uri location = new Uri(context.HttpContext.Request.Url, new Uri(this.Location, UriKind.Relative));

            response.StatusCode = (int)HttpStatusCode.Created;
            response.AppendHeader("Location", location.AbsoluteUri);
            response.ContentType = "text/html";
            response.Cache.SetCacheability(HttpCacheability.NoCache);

            response.Write("<html>");
            response.Write("<head><title>Page created successfully</title>");
            response.Write(String.Format(CultureInfo.InvariantCulture, "<meta http-equiv=\"refresh\" content=\"0;url={0}\">", location.AbsoluteUri));
            response.Write("</head><body>");
            response.Write("<h1>Page created successfully</h1>");
            response.Write("<p>You should be redirected automatically. If not, click the following link:</p>");
            response.Write("<ul><li><a href=\"");
            response.Write(location.AbsoluteUri);
            response.Write("\">");
            response.Write(HttpUtility.HtmlEncode(location.AbsoluteUri));
            response.Write("</a></li></ul>");
            response.Write("</body>");
            response.Write("</html>");
        }
    }
}